import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
// pnpm i -D @types/node
import { resolve } from 'path'
// 自动导入插件
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default ({command, mode}) => {
  console.log("command:", command)
  console.log("mode:", mode)
  const env = loadEnv(mode, process.cwd(), '')
  console.log("env:", env.VITE_BASE_ENV, env.VITE_BASE_URL, env.VITE_HTTP_PROXY)

  return defineConfig({
    base: env.VITE_RES_BASE,
    plugins: [
      vue(),
      AutoImport({
        imports: ['vue'],
        resolvers: [
          ElementPlusResolver(),
        ]
      }),
      Components({
        resolvers: [
          ElementPlusResolver(),
        ],
      }),
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, './src')
      }
    },
    server: {
      host: env.VITE_SERVER_HOST,
      port: Number(env.VITE_SERVER_PORT),
      proxy:{
        [env.VITE_BASE_URL]: {
          target: env.VITE_HTTP_PROXY,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(`^${env.VITE_BASE_URL}/`), ''),
        }
      }
    }
  })
}
